package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsFailedStorage;
import com.jy.wms.pojo.WmsShelves;
import com.jy.wms.service.WmsFailedStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsFailedStorageController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author LH
 * @Date Tue Jan 23 09:31:28 GMT+08:00 2018
 */
@RestController
@RequestMapping("/wmsfailedstorage")
@Api(value = "/wmsfailedstorage", description = "不合格品处理操作（出库）")
public class  WmsFailedStorageController extends BaseController<WmsFailedStorageService,WmsFailedStorage> {


    @PostMapping(value = "/getReceiveList")
    @ApiOperation(value = "查询收货信息（不合格出库页面）",response = JSONObject.class, notes = "查询输入</br>{\"asnId\":\"asnId\"，\"page\":\"页\"，\"rows\":\"行\"}")
    public @ResponseBody JSONObject getReceiveList(@RequestBody Map map, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            List<SysHz> hz = (List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            Page<WmsShelves> list = new Page<WmsShelves>();
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
            String ckmc = userinfo.getUser().getOrgName();//仓库名称
            if(hz.size()>0){
                list= baseService.getReceiveList((String)map.get("asnId"),(Integer)map.get("page"),(Integer)map.get("rows"));
            }
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        } catch (ServiceException e){
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 上架管理 新增上架信息
     * @param list
     * @param session
     * @return
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "不合格品入库 确认入库",response = JSONObject.class, notes = "确认入库")
    public @ResponseBody JSONObject addWmsLibraryPosition(@RequestBody List<WmsShelves> list, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid= String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            String hzid=null;//取当前货主编号
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                hzid=hz.get(0).getHzid();
            }
            baseService.confirm(list,ckid,hzid,session);
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"确认入库信息成功","", "");
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(),"确认入库信息失败",e.getMessage(),"");
        }catch (SucessException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(),"确认入库信息成功",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"确认入库信息失败",e.getMessage(),"");
        }
        return json;
    }





}
